package Cantella::DJabberd::Store::Result::Role;

use base 'DBIx::Class::Core';
use metaclass 'Reaction::Meta::Class';
use Moose;

use MooseX::Types::Moose qw/Int Str ArrayRef/;
use Reaction::Types::Core qw/NonEmptySimpleStr/;

use aliased 'Cantella::DJabberd::Store::Result::UserRole';

use namespace::clean -except => [ 'meta' ];

has id          => (is => 'ro', required => 1, isa => Int );
has name        => (is => 'rw', required => 1, isa => NonEmptySimpleStr );
has description => (is => 'rw', required => 0, isa => Str );
has users   => (
                isa => ArrayRef,
                reader => 'get_users',
                writer => 'set_users'
               );

{
  no warnings 'redefine';
  *get_users = sub { [ shift->users->all ] };
}

__PACKAGE__->table('roles');
__PACKAGE__->add_columns
  (
   id   => {
            data_type => "INT",
            size => 10,
            default_value => undef,
            is_nullable => 0,
            is_auto_increment => 1,
            extra => { unsigned => 1 },
           },
   name => {
            data_type => "VARCHAR",
            size => 255,
            default_value => "",
            is_nullable => 0,
           },
   description => {
                   data_type => "TEXT",
                   size => 65535,
                   default_value => "",
                   is_nullable => 0,
                  },
  );
__PACKAGE__->set_primary_key('id');
__PACKAGE__->add_unique_constraint(name => ['name']);

__PACKAGE__->has_many(user_roles => UserRole, {'foreign.role_id' => 'self.id'});
__PACKAGE__->many_to_many(users => 'user_roles', 'user');

sub display_name { shift->name }

__PACKAGE__->meta->make_immutable(inline_constructor => 0);

1;
